import pandas as pd

SEQ = "MELLILKANAITTILTAVTFCFASGQNITEEFYQSTCSAVSKGYLSALRTGWYTSVITIELSNIKKNKCNGTDAKIKLIKQELDKYKNAVTELQLLMQSTPATNNQARGSGSGRSLGGGGGGGGGGGGGGGGGGGGGGGFLLGVGSAIASGVAVSKVLHLEGEVNKIKSALLSTNKAVVSLSNGVSVLTSKVLDLKNYIDKQLLPIVNKQSCSISNIETVIEFQQKNNRLLEITREFSVNAGVTTPVSTYMLTNSELLSLINDMPITNDQKKLMSNNVQIVRQQSYSIMSIIKEEVLAYVVQLPLYGVIDTPCWKLHTSPLCTTNTKEGSNICLTRTDRGWYCDNAGSVSFFPQAETCKVQSNRVFCDTMNSLTLPSEVNLCNVDIFNPKYDCKIMTSKTDVSSSVITSLGAIVSCYGKTKCTASNKNRGIIKTFSNGCDYVSNKGVDTVSVGNTLYYVNKQEGKSLYVKGEPIINFYDPLVFPSDEFDASISQVNEKINQSLAFIRKSDELLSAIGGYIPEAPRDGQAYVRKDGEWVLLSTFLLVPPGSHHHHHH"

def replace_amino_acid(sequence, position1, position2, new_amino_acid):
   
    # 将序列转换为列表，因为字符串是不可变的
    sequence_list = list(sequence)

    # 替换指定位置的氨基酸
    sequence_list[position1-1] = new_amino_acid
    sequence_list[position2-1] = new_amino_acid

    # 将列表转换回字符串并返回
    return ''.join(sequence_list)


def replace_amino_acid1(sequence, position, new_amino_acid):
   
    # 将序列转换为列表，因为字符串是不可变的
    sequence_list = list(sequence)

    # 替换指定位置的氨基酸
    sequence_list[position-1] = new_amino_acid

    # 将列表转换回字符串并返回
    return ''.join(sequence_list)
cys_pos = [
[154,300],
[158,291],
[158,291],
[158,298],
[164,296],
[168,296],
[170,177],
[170,179],
[198,226],
[237,289],
[243,288],
[244,287],
[257,278],
[258,275],
[280,359],
[280,366],
[288,151],
[300,151],
[302,151],
[32,441],
[75,220],
[78,220],
[82,223],
[82,224],
[82,227],
[85,231],
[86,227],
[89,230],
[89,231],
[89,234],
[92,234],
[92,238],
[93,234],
[93,292],
[94,292],
[96,237],
[96,289],
[97,291],
[97,292],
]
pro_pos = [
211,212,213,214,216
]

cys_list = []

for i in range(len(cys_pos)):
        new_sequence = replace_amino_acid(SEQ, cys_pos[i][0],cys_pos[i][1], "C")
        cys_list.append(new_sequence)
#print(cys_list)





DOUBLE_PRO_list = []
pro_pos = [
    [212,213],
    [213,214],
    [214,215]
]

for c in cys_list:
    for i in range(len(pro_pos)):
        new_sequence = replace_amino_acid(c, pro_pos[i][0],pro_pos[i][1], "P")
        DOUBLE_PRO_list.append(new_sequence)



SINGLE_PRO_list = []
pro_single_pos = [
211,212,213,214,216
]

for p in cys_list:
    for i in pro_single_pos:
        new_sequence = replace_amino_acid1(p, i ,"P")
        SINGLE_PRO_list.append(new_sequence)

all_cys_pro = DOUBLE_PRO_list + SINGLE_PRO_list



############################################################################################################################################

def replace_amino_acid2(sequence, position, new_amino_acid):
   
    # 将序列转换为列表，因为字符串是不可变的
    sequence_list = list(sequence)

    # 替换指定位置的氨基酸
    sequence_list[position-1] = new_amino_acid

    # 将列表转换回字符串并返回
    return ''.join(sequence_list)



position_list = [42,67,74,99,169,172,173,200,201,209,213,228,276,305,326,356,380,384,389,402,462,466,477]
new_amino_acid_list = [
    ["A","R","N","C","Q","H","I","L","F","S","T","W","Y","V"],
    ["E","I","L","F","W","Y"],
    ["D","C","Q","E","H","I","L","F","T","Y"],
    ["A","C","Q","G","H","L","F","T","W","Y"],
    ["R","Q","E","H","I","L","F","T","W","Y","V"],
    ["R","S","T"],
    ["A","R","N","D","C","Q","E","H","I","L","K","F","T","W","Y","V"],
    ["A","R","N","C","Q","E","H","I","L","F","S","T","W","Y","V"],
    ["A","R","N","D","C","Q","E","G","H","I","L","F","S","T","W","Y","V"],
    ["A","R","C","Q","E","H","I","L","F","P","T","W","Y","V"],
    ["R"],
    ["A","R","C","Q","E","G","H","I","L","K","F","S","T","W","Y","V"],
    ["A","Q","E","H","I","L","F","W","Y","V"],
    ["M","Y"],
    ["N","D","C","E","H","I","L","F"],
    ["R","N","Q","H","I","L","F","W","Y","V"],
    ["A","R","C","Q","E","H","I","L","F","S","T","W","Y","V"],
    ["R","H","I","T"],
    ["A","R","N","D","C","Q","E","G","H","I","L","K","F","S","T","W","Y","V"],
    ["E","L"],
    ["R","I","L","W"],
    ["A","N","D","C","Q","H","F","W","Y"],
    ["R","Q","H","I","L","W","Y","V"]
]

seq_list = []

for a in all_cys_pro:
    for i in range(len(position_list)):
        for k in new_amino_acid_list[i]:
            new_sequence = replace_amino_acid2(a, position_list[i], k)
            seq_list.append(new_sequence)

# 输出所有可能的氨基酸序列
#for new_sequence in seq_list:
#    print(new_sequence)       



seq_list2 = []

for x in seq_list:
    for i in range(len(position_list)):
        for k in new_amino_acid_list[i]:
            #print(str(position_list[i])+k)
            new_sequence = replace_amino_acid2(x, position_list[i], k)
            seq_list2.append(new_sequence)

print(len(seq_list))
print(len(seq_list2))
print(len(set(seq_list)))
print(len(set(seq_list2)))


substring = "GGGGGGGGGGGGGGGGGGGGGGG"

# 遍历列表中的每个元素并删除子字符串
for i in range(len(all_cys_pro)):
    all_cys_pro[i] = all_cys_pro[i].replace(substring, "")
for i in range(len(seq_list)):
    seq_list[i] = seq_list[i].replace(substring, "")
for i in range(len(seq_list2)):
    seq_list2[i] = seq_list2[i].replace(substring, "")
print(all_cys_pro)
#print(seq_list)
#print(seq_list2)


dic_cys_pro = {'cys_pro':list(set(all_cys_pro))}
dic_single = {'single':list(set(seq_list))}
dic_double = {'double':list(set(seq_list2))}
df0 = pd.DataFrame(dic_cys_pro)
df = pd.DataFrame(dic_single)
df2 = pd.DataFrame(dic_double)
# write DataFrame to CSV file
df0.to_csv('my_list_cys_pro.csv', index=False)
df.to_csv('my_list_single.csv', index=False)
df2.to_csv('my_list_double.csv', index=False)